from datetime import date, datetime
from typing import Any, Dict, List, Optional
from uuid import uuid4
from pydantic import BaseModel, Field, UUID4, validator, root_validator
from core.logger import logger
from core.utils import en_password, check_password

class UserWechat(BaseModel):
    city : Optional[str] = Field()
    country : Optional[str] = Field()
    headimgurl : Optional[str] = Field()
    nickname : Optional[str] = Field()
    openid : Optional[str] = Field()
    wxid : Optional[UUID4] = Field(...)
    province : Optional[str] = Field()
    sex : Optional[int] = Field(0)

class AccessBase(BaseModel):
    permission_name : Optional[str] = Field()
    permission_desc : Optional[str] = Field()

class RoleBase(BaseModel):
    role_name : Optional[str] = Field()
    access_list: List[AccessBase] = []
    role_status : Optional[bool] = Field(False)
    role_desc : Optional[str] = Field()

class UserRelationBase(BaseModel):
    child_id :  Optional[int] = None
    parent_id :  Optional[int] = None
    relation_type :  Optional[int] = Field(...)
    family: Optional[UUID4] = None
    remarks : Optional[str] = Field()

class UserRelationDel(BaseModel):
    child_id :  Optional[int] = Field(...)
    parent_id :  Optional[int] = Field(...)

class FamilyBase(BaseModel):
    family_id: Optional[UUID4] = None
    family_name: Optional[str] = Field(description="家族名称")
    password : Optional[str] = Field(default="test")
    family_desc: Optional[str] = Field(description="家族描述")
    family_status: Optional[int] = Field(default=1, description="家族状态: 0-解散 1-正常")
    
    @validator("password", pre=False)
    def validator_password(cls, value):
        logger.debug("validator_password")
        return en_password(value)


class UserBase(BaseModel):
    username : Optional[str] = Field(...)
    password : Optional[str] = Field(...)
    user_phone : Optional[str] = Field()
    user_email : Optional[str] = Field()
    full_name : Optional[str] = Field()
    sex : Optional[int] = Field(0)
    birthday: Optional[date] = Field(date(1970, 1, 1))
    user_status : Optional[int] = Field(0)
    header_img : Optional[str] = Field(default="/static/profile/blank.png")
    remarks : Optional[str] = Field()
    wechat_user: Optional[UserWechat] = Field()
    role_list : List[RoleBase] = []
    family: Optional[FamilyBase] = None
    
    @validator("password", pre=False)
    def validator_password(cls, value):
        logger.debug("validator_password")
        return en_password(value)

class UserList(BaseModel):
    page: Optional[int] = 1
    page_size: Optional[int] = 1
    search: Optional[dict] = {}
    order: Optional[list] = []

class UserUpdate(BaseModel):
    user_phone : Optional[str] = Field()
    user_email : Optional[str] = Field()
    full_name : Optional[str] = Field()
    birthday: Optional[date] = Field()
    sex : Optional[int] = Field(0)
    user_status : Optional[int] = Field(0)
    header_img : Optional[str] = Field()
    remarks : Optional[str] = Field()
    wechat_user: Optional[UserWechat] = Field()

class UserBaseOut(BaseModel):
    username : Optional[str] = Field(...)
    user_phone : Optional[str] = Field()
    user_email : Optional[str] = Field()
    full_name : Optional[str] = Field()
    birthday: Optional[date] = Field()
    sex : Optional[int] = Field(0)
    user_status : Optional[int] = Field(0)
    header_img : Optional[str] = Field()
    remarks : Optional[str] = Field()
    wechat_user: Optional[UserWechat] = Field()
    role_list : List[RoleBase] = []
    family: Optional[FamilyBase] = Field()
    
class UserLogin(BaseModel):
    username : Optional[str] = Field(...)
    password : Optional[str] = Field(...)
    
class FamilyLogin(BaseModel):
    family_id : Optional[str] = Field(...)
    password : Optional[str] = Field(...)

class ChangePwdSelf(BaseModel):
    old_pwd : Optional[str] = Field(...)
    new_pwd : Optional[str] = Field(...)

class FamilyOut(BaseModel):
    family_id: UUID4
    family_name: Optional[str] = Field(description="家族名称")
    family_desc: Optional[str] = Field(description="家族描述")
    family_status: Optional[int] = Field( description="家族状态: 0-解散 1-正常")
    members: List[UserBaseOut] = []
    
class UserAddMember(BaseModel):
    username : Optional[str] = Field(...)
    family_id: Optional[UUID4] = Field(...)
    is_create: Optional[UserBase] = None
    child_id: Optional[int] = None
    parent_id: Optional[int] = None
    relation_type: Optional[int] = None